package com.ineatconseil.yougo.pages.usertypes.list;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;

import com.ineatconseil.yougo.beans.UserType;
import com.ineatconseil.yougo.dto.UserTypeDto;
import com.ineatconseil.yougo.exceptions.RestServiceException;
import com.ineatconseil.yougo.pages.AbstractPage;
import com.ineatconseil.yougo.services.rest.RestPath;
import com.sun.jersey.api.client.GenericType;

public class UserTypesList extends AbstractPage {

	@SuppressWarnings("unused")
	@Property
	private UserType userType;

	/**
	 * Get the list.
	 */
	public List<UserType> getUserTypes() {

		try {

			List<UserTypeDto> types =
					getRestServiceInvoker().call(RestPath.getSimplePath(RestPath.PATH_USER_TYPES, ""),
							new GenericType<List<UserTypeDto>>() {
							});

			// Mapp :
			if (types != null) {
				List<UserType> toReturn = new ArrayList<UserType>();
				for (UserTypeDto currentUser : types) {
					UserType req = getMapper().map(currentUser, UserType.class);
					if (!req.isEmpty()) {
						toReturn.add(req);
					}
				}

				if (toReturn.size() == 0) {
					toReturn = null;
				}

				return toReturn;
			}
			return null;

		} catch (RestServiceException e) {
			return null;
		}

	}

	/**
	 * Delete action.
	 */
	@Log
	void onActionFromDelete(long userTypeId) {

		try {

			this.getRestServiceInvoker().delete(
					RestPath.getSimplePath(RestPath.PATH_USER_TYPES, String.valueOf(userTypeId)));

		} catch (RestServiceException e) {

		}

	}

}
